package com.htfyun.eink.pw.configs;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.htfyun.eink.pw.utils.PWUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConfigTopView {

    public interface OnTopViewChangedListener {
        void onTopViewChangedListener(@NonNull ConfigTopView configTopView);
    }

    private final List<OnTopViewChangedListener> onTopViewChangedListeners = new ArrayList<>();

    private final List<Rect> topViewValidRectList = new ArrayList<>();

    /**
     * 在 pw-view 之上的 top-view 里, topView 是普通 view, 不能 写/擦
     */
    private final Map<View, Rect> onTopViewRectMap = new HashMap<>();


    /**
     * 添加 在 pw-view 之上的 top-view, 这样可以在此 top-view 上继续传递 touch 事件
     *
     * @param view 在 pw-view 之上的 top-view
     */
    public void addOnTopView(@Nullable View view) {
        if (view == null) {
            return;
        }

        doAddOnTopView(view);

        view.removeOnLayoutChangeListener(onLayoutChangeListener);
        view.addOnLayoutChangeListener(onLayoutChangeListener);
    }


    /**
     * 删除 在 pw-view 之上的 top-view
     *
     * @param view 在 pw-view 之上的 top-view
     */
    public void removeOnTopView(@Nullable View view) {
        if (view == null) {
            return;
        }
        doRemoveOnTopView(view);
    }

    @NonNull
    public Map<View, Rect> getOnTopViewRectMap() {
        return onTopViewRectMap;
    }

    @NonNull
    public List<Rect> getTopViewValidRectList() {
        return topViewValidRectList;
    }

    public void addOnTopViewChangedListener(@NonNull OnTopViewChangedListener listener) {
        if (!onTopViewChangedListeners.contains(listener)) {
            onTopViewChangedListeners.add(listener);
        }
        listener.onTopViewChangedListener(this);
    }

    public void removeOnTopViewChangedListener(@NonNull OnTopViewChangedListener listener) {
        onTopViewChangedListeners.remove(listener);
    }

    private void notifyTopViewChanged() {

        topViewValidRectList.clear();

        Map<View, Rect> onTopViewRectMap = getOnTopViewRectMap();
        if (!onTopViewRectMap.isEmpty()) {
            topViewValidRectList.addAll(onTopViewRectMap.values());
        }

        for (OnTopViewChangedListener listener : onTopViewChangedListeners) {
            listener.onTopViewChangedListener(this);
        }
    }

    private void doAddOnTopView(@NonNull View view) {
        onTopViewRectMap.put(view, PWUtils.getRectOfViewOnScreen(view));
        notifyTopViewChanged();
    }

    public void doRemoveOnTopView(@NonNull View view) {
        onTopViewRectMap.remove(view);
        notifyTopViewChanged();
    }

    private final View.OnLayoutChangeListener onLayoutChangeListener = new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            if (v != null) {
                doAddOnTopView(v);
            }
        }
    };
}
